package com.iexbuy.ihg.ui.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.baidu.navisdk.util.common.StringUtils;
import com.iexbuy.ihg.App;
import com.iexbuy.ihg.R;
import com.iexbuy.ihg.common.CommUtil;
import com.iexbuy.ihg.entity.ShopItemDetailObject;
import com.iexbuy.ihg.entity.ShopItemListObject;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.utils.JsonUtils;
import com.iexbuy.ihg.utils.UnitUtils;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.math.BigDecimal;
import java.util.HashMap;

public class HuanGouItemDetailFragment extends BaseFragment {
	
	private static HuanGouItemDetailFragment homeFragmentInstent;

	@ViewInject(R.id.msgListLayout)
	private PullToRefreshLayout msgListRefreshLayout;
	@ViewInject(R.id.listView)
	private ListView listView;

	private LinearLayout listHeaderView;
	@ViewInject(R.id.shopIcon)
	private ImageView shopIcon;
	@ViewInject(R.id.shopTitle)
	private TextView shopTitle;
	@ViewInject(R.id.shop_sub_title)
	private TextView shop_sub_title;
	@ViewInject(R.id.currentPrice)
	private TextView currentPrice;
	@ViewInject(R.id.transformPrice)
	private TextView transformPrice;
	@ViewInject(R.id.buyBtn)
	private Button buyBtn;
	@ViewInject(R.id.last_comment)
	private TextView last_comment;
	@ViewInject(R.id.shopStar)
	private TextView shopStar;
	@ViewInject(R.id.webView)
	private WebView webView;
	@ViewInject(R.id.scrollview_shop_detail)
	private ScrollView scrollview_shop_detail;
	@ViewInject(R.id.buttonBuyLayout1)
	private RelativeLayout buttonBuyLayout1;
	@ViewInject(R.id.buttonBuyLayout2)
	private RelativeLayout buttonBuyLayout2;
	@ViewInject(R.id.currentPrice2)
	private TextView currentPrice2;
	@ViewInject(R.id.transformPrice2)
	private TextView transformPrice2;
	@ViewInject(R.id.buyBtn2)
	private Button buyBtn2;

	private static ShopItemListObject shopItemListObject;
	private ShopItemDetailObject shopItemDetailObject;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		homeFragmentInstent = this;
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_shop_item_detail, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View rootView) {

		mBaseActivity.setTitleLayoutVisibility(true);
		mBaseActivity.setHomeActivity(false);

		buyBtn.setText("立即换购");
		transformPrice.setVisibility(View.GONE);
		transformPrice2.setVisibility(View.GONE);
		refreshDate();
		initScrollView();

	}

	/**
	 * 滑动到一定距离显示btn
	 */

	private void initScrollView() {

		scrollview_shop_detail.setOnTouchListener(new View.OnTouchListener(){

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction()==MotionEvent.ACTION_MOVE){
					int scollY =scrollview_shop_detail.getScrollY();
					int topY  =  buttonBuyLayout1.getTop()+buttonBuyLayout1.getHeight();

					if(scollY>topY){
						buttonBuyLayout2.setVisibility(View.VISIBLE);
					}else{
						buttonBuyLayout2.setVisibility(View.GONE);
					}
				}

				return false;
			}
		});
	}

	//刷新整个页面数据
	private void refreshDate() {
		initShopItemDetailData();
	}

	//获取商家详情
	private void initShopItemDetailData() {
		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("goodsId", shopItemListObject.getItemId());
		ServiceHelper.send(mBaseActivity, ServiceHelper.GET_GOODS_DETAIL, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
					}

					@Override
					public void onCancelled() {
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						shopItemDetailObject = JsonUtils.parseJsonStrToObject(JsonUtils.getJsonSubStringByKey(responseInfo.result, "body"), ShopItemDetailObject.class);
						initShopDetailView();
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
					}
				});
	}

	private void initShopDetailView() {
		if (shopItemDetailObject == null) return;
		//分页
		mBaseActivity.setTitle(shopItemDetailObject.getItemName());
		App.bitmapUtils.display(shopIcon, shopItemDetailObject.getThumbnailUrl());
		shopTitle.setText(shopItemDetailObject.getItemName());

		shop_sub_title.setText(shopItemDetailObject.getSummary());
		currentPrice.setText("换购价格："+shopItemDetailObject.getNewPrice()+"金币");
		currentPrice2.setText("换购价格："+shopItemDetailObject.getNewPrice()+"金币");
		transformPrice.setText(shopItemDetailObject.getExpressType()+"："+String.valueOf(shopItemDetailObject.getExpressPrice())+"元");
		transformPrice2.setText(shopItemDetailObject.getExpressType()+"："+String.valueOf(shopItemDetailObject.getExpressPrice())+"元");

		last_comment.setText("最新评价：" + (StringUtils.isNotEmpty(shopItemDetailObject.getLastComment())? shopItemDetailObject.getLastComment() : "暂无评价"));
		shopStar.setText(new BigDecimal(shopItemDetailObject.getRank()).setScale(1, BigDecimal.ROUND_HALF_DOWN).toString());

		if ("1".equalsIgnoreCase(shopItemDetailObject.getPayId())) {//金币
			currentPrice.setTextSize(UnitUtils.dip2px(mBaseActivity, 18));
		}
		//商品详情html
		webView.getSettings().setDefaultTextEncodingName("UTF-8");
		webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
		webView.getSettings().setUseWideViewPort(true);
		webView.getSettings().setLoadWithOverviewMode(true);
		shopItemDetailObject.setDetail(shopItemDetailObject.getDetail().replaceAll("\\<img ", "\\<img width='100%' "));
		webView.loadData(shopItemDetailObject.getDetail(), "text/html; charset=UTF-8", null);

	}

	//页面响应
	@OnClick(R.id.buyBtn)
	public void onBuyBtnClick(View view) {
		shopItemDetailObject.setShop(shopItemListObject.getShop());
		HuanGouOrderConfirmFragment.setShopItemDetailObject(shopItemDetailObject);
		CommUtil.openSingleFragmentActivity(mBaseActivity, HuanGouOrderConfirmFragment.class);
	}
	@OnClick(R.id.buyBtn2)
	public void onBuyBtn2Click(View view) {
		shopItemDetailObject.setShop(shopItemListObject.getShop());
		HuanGouOrderConfirmFragment.setShopItemDetailObject(shopItemDetailObject);
		CommUtil.openSingleFragmentActivity(mBaseActivity, HuanGouOrderConfirmFragment.class);
	}

	public static void setShopItemListObject(ShopItemListObject shopItemListObject) {
		HuanGouItemDetailFragment.shopItemListObject = shopItemListObject;
	}

	/**
	 * @return the homeFragmentInstent
	 */
	public static HuanGouItemDetailFragment getHomeFragmentInstent() {
		return homeFragmentInstent;
	}

}
